package com.im.partchat.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.im.partchat.R;
import com.im.partchat.bean.main.MainGirdBean;

import java.util.List;

/**
 * Created by fzl on 2017/4/14.
 * VersionCode: 1
 * Desc:
 */

public class MainGirdAdapter extends BaseAdapter {
    private Context mContext;
    private List<MainGirdBean> mainTypeBeanList;
    private OnItemClickListener onItemClickListener;


    public MainGirdAdapter(Context context, List<MainGirdBean>beanList) {
        this.mContext = context;
        this.mainTypeBeanList = beanList;


    }

    @Override
    public int getCount() {
        return mainTypeBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return mainTypeBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (null == convertView) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.view_main_gird_item, null);

            holder.imageView = (ImageView) convertView.findViewById(R.id.all_type_image);
            holder.textView = (TextView) convertView.findViewById(R.id.all_type_text);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            if (onItemClickListener!=null){
                onItemClickListener.onItemClickListener(position);
            }

            }
        });
        holder.imageView.setImageResource(mainTypeBeanList.get(position).getResId());
        holder.textView.setText(mainTypeBeanList.get(position).getTitle());
        return convertView;
    }

    public class ViewHolder {
        private ImageView imageView;
        private TextView textView;

    }

    public void  setOnItemClickListener(OnItemClickListener listener){
        this.onItemClickListener=listener;
    }
    public interface OnItemClickListener{
       void  onItemClickListener(int Position);
    }
}
